package com.vijay.tweet;

import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MyTweet2Activity extends ListActivity {
	public ImageLoader imageLoader;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imageLoader = new ImageLoader(MyTweet2Activity.this.getApplicationContext());
        
        new MyTask().execute();
        
        
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	
    }
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	setListAdapter(null);
    	imageLoader.clearCache();
    }
    
    public class MyTask extends AsyncTask<Void, Void, Void>{
    	
    	private ProgressDialog dialog;
    	public ArrayList<Tweet> tweets= new ArrayList<Tweet>();
    	@Override
    	protected void onPreExecute() {
    	dialog = ProgressDialog.show(MyTweet2Activity.this, " ","Please wait...",  true);
    	}
		@Override
		protected Void doInBackground(Void... params) {
			try {
                HttpClient hc = new DefaultHttpClient();
                HttpGet get = new
                HttpGet("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=chinmayi&count=30"); 
                HttpResponse rp = hc.execute(get);
                if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                {
                        String result = EntityUtils.toString(rp.getEntity());
                        System.out.println(result);
                        
                        JSONArray jsonReply = new JSONArray(result); 
                		//Movie movie = new Movie();
                		//JSONObject jsonMovie =  
                        for(int i = 0 ; i<29 ; i++){
                        	
                        	JSONObject root = jsonReply.getJSONObject(i);
                            System.out.println("JSONObject"+root.toString());
                            Tweet tweet = new Tweet();
                            tweet.msg = root.getString("text");  
                            tweet.name= root.getJSONObject("user").getString("name");
                            tweet.URL = root.getJSONObject("user").getString("profile_image_url_https");
                            //tweet.name = root.getString("from_user");                                 
                            //tweet.URL = root.getString("profile_image_url");
                            
                            /*JSONArray sessions = root.getJSONArray("results");
                            for (int i = 0; i < sessions.length(); i++) {
                                    JSONObject session = sessions.getJSONObject(i);
                                     Tweet tweet = new Tweet();
                                     tweet.msg = session.getString("text");                                 
                                     tweet.name = session.getString("from_user");                                 
                                     tweet.URL = session.getString("profile_image_url");
                                     tweets.add(tweet);
                            }*/
                            tweets.add(tweet);
                        	
                        }
                        
               }
       } catch (Exception e) {
               Log.e("TwitterFeedActivity", "Error loading JSON", e);
       }
       return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
		            dialog.dismiss();
	                setListAdapter(new TweetListAdaptor(MyTweet2Activity.this, R.layout.feed, tweets));
	    
			
		}
    	
    }
    public class TweetListAdaptor extends ArrayAdapter<Tweet> {
        private ArrayList<Tweet> tweets;
        public TweetListAdaptor(Context context,int textViewResourceId, ArrayList<Tweet> items) {
                  super(context, textViewResourceId, items);
                  this.tweets = items;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                        LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        v = vi.inflate(R.layout.feed, null);
                }
                Tweet o = tweets.get(position);
                TextView tt = (TextView) v.findViewById(R.id.username);
                TextView bt = (TextView) v.findViewById(R.id.message);
                ImageView iv = (ImageView) v.findViewById(R.id.avatar);
                ProgressBar pb = (ProgressBar) v.findViewById(R.id.loading);
                tt.setText(o.name);
                bt.setText(o.msg);
                imageLoader.DisplayImage(o.URL, iv , pb);
                
                return v;
        }
   }
    
    public class Tweet{
    	public String name;
    	public String msg;
    	public String URL;
    }
    


}